package presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;

public class PSampleTimingLabel extends JLabel implements Observer {
	private static final long serialVersionUID = 1L;
	private String prefix, unit;
	private DecimalFormat df = new DecimalFormat("#");
	private Long lastValue = 0l;

	public PSampleTimingLabel(String prefix, String text, String unit, 
			int horizontalAlignment, Font font,  int width, int height) {
		super(prefix + text + unit, horizontalAlignment);
		this.setPreferredSize(new Dimension(width,height));
		this.prefix = prefix;
		this.unit = unit;
		this.setFont(font);
	}

	@Override
	public void update(Observable obj, Object arg) {
		if (arg instanceof Number) {
			Long newValue = (Long) arg;
			Long time = (newValue - lastValue)*2;
			
			this.setText(prefix + df.format(time) + unit);
			
			lastValue = newValue;
		}
	}
}